import schedule
import time


def email():
    number = '你的QQ邮箱号码'
    smtp = '邮箱对应的STMP授权码'
    to = '需要发送到的QQ邮箱号码'  # 可以是非QQ的邮箱

    mer = MIMEMultipart()
    # 设置邮件正文内容
    head = '''
    <p>微博热搜榜信息</p>
    <p>最热门词条为</p>
    <p><a href="{}">{}</a></p>
    <p>排名前五的热搜</p>
    <p><a href="{}">{}</a></p>
    <p><a href="{}">{}</a></p>
    <p><a href="{}">{}</a></p>
    <p><a href="{}">{}</a></p>
    <p><a href="{}">{}</a></p>
    '''.format(df.iloc[0, :]['href'], df.iloc[0, :]['关键词'],
               df.iloc[1, :]['href'], df.iloc[1, :]['关键词'],
               df.iloc[2, :]['href'], df.iloc[2, :]['关键词'],
               df.iloc[3, :]['href'], df.iloc[3, :]['关键词'],
               df.iloc[4, :]['href'], df.iloc[4, :]['关键词'],
               df.iloc[5, :]['href'], df.iloc[5, :]['关键词'])
    mer.attach(MIMEText(head, 'html', 'utf-8'))
    fujian = MIMEText(open('微博热搜.csv', 'rb').read(), 'base64', 'utf-8')
    fujian["Content-Type"] = 'application/octet-stream'  # 附件内容
    fujian.add_header('Content-Disposition', 'file', filename=('utf-8', '', '微博热搜.csv'))
    mer.attach(fujian)

    mer['Subject'] = '每日微博热搜榜单'  # 邮件主题
    mer['From'] = number  # 发送人
    mer['To'] = to  # 接收人

    # 5.发送邮件
    s = smtplib.SMTP_SSL('smtp.qq.com', 465)
    s.login(number, smtp)
    s.send_message(mer)  # 发送邮件
    s.quit()
    print('成功发送')


schedule.every().day.at("20:00").do(email)
while True:
    schedule.run_pending()
    time.sleep(5)